package com.flyox.game.militarychess.server.services;

import java.io.IOException;
import java.io.OptionalDataException;
import java.util.Hashtable;

import com.flyox.game.militarychess.CONSTS;
import com.flyox.game.militarychess.bean.ChessMan;
import com.flyox.game.militarychess.bean.Player;
import com.flyox.game.militarychess.bean.ChessMan.NAME;
import com.flyox.game.militarychess.server.services.data.SystemInfo;

public class LayoutService {
	
	SystemInfo systemInfo=new SystemInfo();
	/**
	 * 得到某一桌的桌面布局
	 * @param dskNum
	 * @return
	 */
	public ChessMan[][] getDeskLayout(int deskNum){
		if(deskNum<0||deskNum>CONSTS.defaultDeskNum){
			return new ChessMan[13][20];
		}
		ChessMan[][] layout=systemInfo.getChessLayouts().get(deskNum);
		if(layout==null){
			return new ChessMan[13][20];
		}
		return layout;
	}
	
	/**
	 * 得到player可以看到的桌面布局
	 * @param p
	 * @return
	 */
	public ChessMan[][] getDeskLayout(Player p){
		int dskNum=p.getDeskNum();
		ChessMan[][] serverTemp=getDeskLayout(dskNum);
		ChessMan[][] res=new ChessMan[13][20];
		
		for(int i=0;i<13;i++){
			for(int j=0;j<20;j++){
				//不为空 并且不是自己的棋子 设为 unknow 判断是否可以显示军旗
				if(serverTemp[i][j]==null||serverTemp[i][j].getName().equals(NAME.EMPTY)){
					continue;
				}
				try {
					res[i][j]=serverTemp[i][j].deepClone();
				} catch (OptionalDataException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
				//不是自己的棋 并没有查看权限的
				if(!res[i][j].getTeam().equals(p.getSeatName())&&(new DeskService()).getShowLevel(dskNum).compareTo(res[i][j].getShowLevel())>=0){
					res[i][j].setName(NAME.UNKNOW);
				}
			}//end for col
		}//end for row
		return res;
	}
	
	/**
	 * 得到所有布局
	 * @return
	 */
	public Hashtable<Integer, ChessMan[][]> getChessLayouts() {
		return systemInfo.getChessLayouts();
	}
	
	/*
	 * 初始化桌面布局
	 */
	public void initChessLayout(Integer deskNum){
		if(systemInfo.getChessLayouts().get(deskNum)==null){
			systemInfo.getChessLayouts().put(deskNum, new ChessMan[13][21]);
		}else{
			systemInfo.getChessLayouts().remove(deskNum);
			systemInfo.getChessLayouts().put(deskNum, new ChessMan[13][21]);
		}
	}
}
